package jp.sourceforge.akjrcp.videotools.spumux;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import jp.sourceforge.akjrcp.akj_betas.wizards.DVDStylerExportWizard;

import org.apache.commons.io.FileUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class SpumuxUtils {
public static Document toDocument(TextSub textSub){
	Document document=null;
	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       
    DocumentBuilder builder=null;
	try {
		builder = factory.newDocumentBuilder();
		document = builder.newDocument();  // Create from whole cloth
		 
		 
		 

		 Element root=document.createElement("subpictures");
		 Element stream=document.createElement("stream");
		 root.appendChild(stream);
		 
		 Element tsub=document.createElement("textsub");
		 stream.appendChild(tsub);
		 
		 if(textSub.getFilename()==null){
			 return null;
		 }else{
			 tsub.setAttribute("filename", textSub.getFilename());
		 }
		 
		 tsub.setAttribute("fontsize", ""+textSub.getFontsize());
		 tsub.setAttribute("font", ""+textSub.getFont());
		 tsub.setAttribute("horizontal-alignment", ""+textSub.getHorizontal_alignment());
		 tsub.setAttribute("vertical-alignment", ""+textSub.getVertical_alignment());
		 tsub.setAttribute("left-margin", ""+textSub.getLeft_margin());
		 tsub.setAttribute("right-margin", ""+textSub.getRight_margin());
		 tsub.setAttribute("top-margin", ""+textSub.getTop_margin());
		 tsub.setAttribute("bottom-margin", ""+textSub.getBottom_margin());
		 tsub.setAttribute("subtitle-fps", ""+textSub.getSubtitle_fps());
		 tsub.setAttribute("movie-fps", ""+textSub.getMovie_fps());
		 tsub.setAttribute("movie-width", ""+textSub.getMovie_width());
		 tsub.setAttribute("movie-height", ""+textSub.getMovie_height());
		 if(textSub.isForce()){
			 tsub.setAttribute("force", "yes"); 
		 }else{
			 tsub.setAttribute("force", "no");
		 }
		 
		 document.appendChild(root);
	}catch(Exception e){
		e.printStackTrace();
	}
	return document;
}

public static String[] toSpumuxCommand(String spumuxExePath,String inputVob,String outputVob,int id,String spumuxFilePath){
	String batPath="";
	
	try {
		File tmp=File.createTempFile("spumux", ".bat");
		FileUtils.writeStringToFile(tmp, "\""+spumuxExePath+"\""+" "+"-s"+" "+""+id+" "+"\""+spumuxFilePath+"\""+" "+"<"+"\""+inputVob+"\""+" "+">"+"\""+outputVob+"\"");
		tmp.deleteOnExit();
		batPath=tmp.getAbsolutePath();
	} catch (IOException e) {
		e.printStackTrace();
	}
//	cm.exe should better exec only one command or bat
	String command[]=new String[]{"cmd.exe","/c",batPath};
	
	return command;
}

public static void writeXML(Document document,String path){
	  DVDStylerExportWizard.log.info(path);
	  DVDStylerExportWizard.log.info(document);
    XMLSerializer serial=new XMLSerializer();
    Writer writer=null;
    try {
  	 OutputFormat format=new OutputFormat();
  	 format.setEncoding("UTF-8");
  	 format.setLineSeparator("\r\n");
  	 format.setIndenting(true);
  	 serial.setOutputFormat(format);
  	 writer=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path)),"UTF-8"));
		 serial.setOutputCharStream(writer);
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		 DVDStylerExportWizard.log.info(e1.getMessage());
		e1.printStackTrace();
	}
    try {
  	  DVDStylerExportWizard.log.info("start serialize");
		serial.serialize(document);
		 DVDStylerExportWizard.log.info("end serialize");
		writer.flush();
		writer.close();
	} catch (IOException e) {
		DVDStylerExportWizard.log.info(e.getMessage());
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
}
